// pages/goods/result/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchValue: '',
    activeFilter: 'all', // 默认选中“综合”
    priceSort: 'desc',     // 默认价格排序为降序
    showModal: false,
    minPrice: null,
    maxPrice: null,
    loadMore: false,
    noMore: false,
    page: 1,
    pageSize: 10,
    goodList: [
      {
        id: 0, name: '裙子加快速度放假啦司法拘留苏卡达飞机卡手就开始进度款发件方', tag: '2020秋季新款2020秋季新款2020秋季新款2020秋季新款2020秋季新款', salePrice: 300, originalPrice: 350, 
        imageUrl: 'https://t14.baidu.com/it/u=2337417995,3682422048&fm=224&app=112&size=h200&n=0&f=JPEG&fmt=auto?sec=1733245200&t=195a24f901f0a8fbc4f2f946164fa046'
      },
      {
        id: 1, name: '裙子1', tag: '限时特惠', salePrice: 300, originalPrice: 350, 
        imageUrl: 'https://t14.baidu.com/it/u=2337417995,3682422048&fm=224&app=112&size=h200&n=0&f=JPEG&fmt=auto?sec=1733245200&t=195a24f901f0a8fbc4f2f946164fa046'
      },
      {
        id: 2, name: '裙子2', tag: '限时特惠', salePrice: 300, originalPrice: 350, 
        imageUrl: 'https://t14.baidu.com/it/u=2337417995,3682422048&fm=224&app=112&size=h200&n=0&f=JPEG&fmt=auto?sec=1733245200&t=195a24f901f0a8fbc4f2f946164fa046'
      },
      {
        id: 3, name: '裙子3', tag: '限时特惠', salePrice: 300, originalPrice: 350, 
        imageUrl: 'https://t14.baidu.com/it/u=2337417995,3682422048&fm=224&app=112&size=h200&n=0&f=JPEG&fmt=auto?sec=1733245200&t=195a24f901f0a8fbc4f2f946164fa046'
      },
      {
        id: 4, name: '裙子4', tag: '限时特惠', salePrice: 300, originalPrice: 350, 
        imageUrl: 'https://t14.baidu.com/it/u=2337417995,3682422048&fm=224&app=112&size=h200&n=0&f=JPEG&fmt=auto?sec=1733245200&t=195a24f901f0a8fbc4f2f946164fa046'
      },
      {
        id: 5, name: '裙子5', tag: '限时特惠', salePrice: 300, originalPrice: 350, 
        imageUrl: 'https://t14.baidu.com/it/u=2337417995,3682422048&fm=224&app=112&size=h200&n=0&f=JPEG&fmt=auto?sec=1733245200&t=195a24f901f0a8fbc4f2f946164fa046'
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('搜索结果页面');
    // console.log(options.goodsName);
    let searchValue = options.goodsName;
    this.setData({searchValue})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },
  /**
   * 筛选项点击
   * @param {*} event 
   */
  onFilterTap(event) {
    const filter = event.currentTarget.dataset.filter;
    if (filter === 'filter') {
      //筛选栏显示
      this.setData({
        activeFilter: filter,
        priceSort: 'desc',
        showModal: true
      });
    } else {
      //之前已经是价格
      const isAlreadyPrice = this.data.activeFilter === 'price';
      //价格点击
      const isPriceTap = filter === 'price';
      this.setData({
        activeFilter: filter,
        priceSort: filter === 'price' ? this.data.priceSort : 'desc', // 切换到其他筛选项时重置价格排序
        showModal: false
      });
      if(isPriceTap && isAlreadyPrice){
        this.onPriceSortTap(event);
      }
    }
  },
  /**
   * 价格筛选变更
   */
  onPriceSortTap(event) {
    const sort = event.currentTarget.dataset.sort;
    this.setData({
      priceSort: sort === 'desc' ? 'asc' : 'desc'
    });
  },

  onResetFilter() {
    this.setData({
      minPrice: null,
      maxPrice: null
    });
  },

  onConfirmFilter(event) {
    const { minPrice, maxPrice } = event.detail;
    this.setData({
      minPrice,
      maxPrice
    });
    // 在这里可以处理筛选后的逻辑，例如重新加载商品列表
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom(){
    console.log('加载更多');
    this.setData({
      loadMore: true,
      noMore: false,
      page: this.data.page + 1
    });
    var newItems = [];
    var noMoreTemp = false;
    if(this.data.page < 3){
      for (let index = 0; index < this.data.pageSize; index++) {
        newItems[index] = {
            id: 0, name: '裙子'+(this.data.pageSize * this.data.page + index + 1), tag: '2020秋季新款', salePrice: 300, originalPrice: 350, 
          imageUrl: 'https://t14.baidu.com/it/u=2337417995,3682422048&fm=224&app=112&size=h200&n=0&f=JPEG&fmt=auto?sec=1733245200&t=195a24f901f0a8fbc4f2f946164fa046'
        };
      }
    }else{
      noMoreTemp = true;
    }
    setTimeout(() => {
      this.setData({
        loadMore: false,
        noMore: noMoreTemp,
        goodList: this.data.goodList.concat(newItems)
      });
    }, 2000);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})